package cn.thornbird.orgsync.config;

import cn.thornbird.orgsync.exception.*;
import cn.thornbird.orgsync.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Result<?> exceptionHandler(HttpServletRequest request, HttpMessageNotReadableException exception) {
        log.error(String.format("%s NOT READABLE", request.getRequestURI()), exception);
        return Result.error(HttpStatus.BAD_REQUEST.value(), "JSON parse error");
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result<?> exceptionHandler(HttpServletRequest request, MethodArgumentNotValidException exception) {
        log.error(String.format("%s NOT VALID", request.getRequestURI()), exception);
        String message = "Validation failed: " + exception.getBindingResult().getObjectName();
        if (exception.getBindingResult().getErrorCount() > 0) {
            message = exception.getBindingResult().getAllErrors().get(0).getDefaultMessage();
        }
        return Result.error(HttpStatus.BAD_REQUEST.value(), message);
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(IllegalArgumentException.class)
    public Result<?> exceptionHandler(HttpServletRequest request, IllegalArgumentException exception) {
        log.error(String.format("%s NOT VALID", request.getRequestURI()), exception);
        return Result.error(HttpStatus.BAD_REQUEST.value(), "Illegal argument");
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(BadRequestException.class)
    public Result<?> exceptionHandler(HttpServletRequest request, BadRequestException exception) {
        log.error(String.format("%s BAD REQUEST", request.getRequestURI()), exception);
        return Result.error(exception.getStatusCode(), exception.getMessage());
    }

    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(UnauthorizedException.class)
    public Result<?> exceptionHandler(HttpServletRequest request, UnauthorizedException exception) {
        log.error(String.format("%s UNAUTHORIZED", request.getRequestURI()), exception);
        return Result.error(exception.getStatusCode(), exception.getMessage());
    }

    @ResponseStatus(HttpStatus.FORBIDDEN)
    @ExceptionHandler(ForbiddenException.class)
    public Result<?> exceptionHandler(HttpServletRequest request, ForbiddenException exception) {
        log.error(String.format("%s FORBIDDEN", request.getRequestURI()), exception);
        return Result.error(exception.getStatusCode(), exception.getMessage());
    }

    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NotFoundException.class)
    public Result<?> exceptionHandler(HttpServletRequest request, NotFoundException exception) {
        log.error(String.format("%s NOT FOUND", request.getRequestURI()), exception);
        return Result.error(exception.getStatusCode(), exception.getMessage());
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(InternalServerErrorException.class)
    public Result<?> exceptionHandler(HttpServletRequest request, InternalServerErrorException exception) {
        log.error(String.format("%s INTERNAL SERVER ERROR", request.getRequestURI()), exception);
        return Result.error(exception.getStatusCode(), exception.getMessage());
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public Result<?> exceptionHandler(HttpServletRequest request, Exception exception) {
        log.error(String.format("%s INTERNAL SERVER ERROR", request.getRequestURI()), exception);
        return Result.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Internal Server Error");
    }

}
